<?php 
    include_once 'functionsDocs.php';
	include_once 'functions_db.php';
	
	//id document
	$idd=$_GET[iddoc];
	$idp=$_GET[idp];

	$dbh=dbConn();

	define('_MPDF_PATH','mpdf/');
	include('mpdf/mpdf.php');
	
    $mpdf=new mPDF('win-1252','A4','','',15,15,35,16, 1,8); //class mPDF ([ string $codepage [, mixed $format [, float $default_font_size [, string $default_font [, float $margin_left , float $margin_right , float $margin_top , float $margin_bottom , float $margin_header , float $margin_footer [, string $orientation ]]]]]])
	
	//preia logo firme supplier si client
	$rescomp=returnQuery("Select c.id, c.picture, c.name, pc.id_company_type from companies c, project_companies pc where pc.id_project=".$idp." and c.id=pc.id_company;",$dbh);
	//static $j=0; 
	$j=0;
	while (($rowcomp=mysql_fetch_assoc($rescomp))&&($j<2))
		{
			if ($rowcomp['id_company_type']==0)
				{
				$pic_supplier=$rowcomp['picture'];
				$k=1;
				}
			else
			{
				$pic_client=$rowcomp['picture'];
				$i=1;
				$client = $rowcomp['name'];
				}
				$j=k+i;
				
		
		}
		
			
	//echo "<img src='".$row['picture']."' width='50' height='50' /> </td></tr>";
	
	//preia nume proiect
	$result2 = returnQuery("select * from projects where id='".$idp."';", $dbh);
	$row2 = mysql_fetch_assoc($result2);
	
	//preia nume document si data
	$result3=mysql_query("select * from documents where id=".$idd.";");
	$rowd=mysql_fetch_assoc($result3);
	
	$ts_date = strtotime($rowd['date']);
    $data=date('d-m-Y',$ts_date);
	
	//document de tip acceptance sau  nu
	$acc = $rowd['for_acceptance'];

	//set header

$header = '
<table width="100%">
  <tr>
    <td align="left"><img src='.$pic_client.' height="90px"></td>
    <td align="right"><img src='.$pic_supplier.' height="90px"></td>
  </tr>
  </table>';
  
$info = '
<table width="100%" style="font-size: 12pt; font-weight:bold;">
  <tr>
    <td align="left">Project name: '.$row2["name"].'</td>
	</tr>
  <tr>
	<td align="left">Customer: '.$client.'</td>
  </tr>
  </table>';
 
$titlu = '
  <table width="100%" style="font-size: 20pt; font-weight:bold;">
  <tr>
    <td align="center">'.$rowd["name"].'</td>
  </tr>
  </table>';
  
 /* <table width="100%" style="font-size: 8pt;">
  <tr>
    <td align="right">'.$data.'</td>
  </tr>
</table>'; */


    $mpdf->SetHTMLHeader($header);
	
	$mpdf->WriteHTML('<br>');
	
	$mpdf->WriteHTML($info);
	
	$mpdf->WriteHTML($titlu);

    // footer
	$mpdf->SetFooter('{PAGENO}/{nb}');
	
   // $mpdf->AddPage();
	
    $result = returnQuery("select *, concat(orderT,'.',orderS,'.',orderC) orderby from document_body where id_doc='".$idd."' order by orderT,orderS, orderby;", $dbh);
	
	while ($row = mysql_fetch_assoc($result))
	{	
	
	
	if ($row["type"]=="Title")
	{
	
	$mpdf->WriteHTML('<h3>'.$row["content"].'</h3>');
	}
	
	else if ($row["type"]=="Subtitle")	
	{
	
	$mpdf->WriteHTML('<h4>'.$row["content"].'</h4>');
	}
	
    else if ($row["type"]=="Content")
	{
	$mpdf->WriteHTML('<div style="text-align:justify;">'.$row["content"].'</div>');
	}
	
	
	}

    //adauga sectiune pentru acceptance daca documentul este de tip acceptance
	//de tratat pozitionarea in pagina a sectiunii de acceptance
	
	if ($acc == 1)
	{
	$acceptance_section = '
	<br><br>
	<p style:"vertical-align:bottom;">
	<table width="100%" border="0" style:"vertical-align:bottom;">
  <tr>
    <td width="30%">Date:</td>
    <td width="70%">Signature:</td>
  </tr>
</table>
</p>';
    $mpdf->WriteHTML($acceptance_section);
	}
	
	
	//output
	$mpdf->Output('nume_document.pdf','D');
	exit;

	dbClose($dbh);
?> 